-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasicStatistics.cpp
More file actions
383 lines (325 loc) · 9.33 KB
/
BasicStatistics.cpp
File metadata and controls
383 lines (325 loc) · 9.33 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
#include <algorithm>
#include <numeric>
#include <cmath>
#include <limits>
#include "BasicStatistics.h"
#include "Exceptions.h"
QVector<double> BasicStatistics::factorial_cache = QVector<double>();
const int LOG_FACTORIAL_CACHE_SIZE = 120000;
QVector<double> BasicStatistics::log_factorial_cache = QVector<double>();
double BasicStatistics::mean(const QVector<double>& data)
{
return mean(data, 0, data.size() - 1);
}
double BasicStatistics::mean(const QVector<double>& data, int start_index, int end_index)
{
const int n = end_index - start_index + 1;
if (n==0)
{
THROW(StatisticsException, "Cannot calculate mean on empty data array.");
}
return std::accumulate(data.begin() + start_index, data.begin() + (end_index + 1), 0.0) / n;
}
double BasicStatistics::stdev(const QVector<double>& data)
{
return stdev(data, mean(data));
}
double BasicStatistics::stdev(const QVector<double>& data, double mean)
{
return stdev(data, mean, 0, data.size() - 1);
}
double BasicStatistics::stdev(const QVector<double>& data, double mean, int start_index, int end_index)
{
const int n = end_index - start_index + 1;
if (n==0)
{
THROW(StatisticsException, "Cannot calculate standard deviation on empty data array.");
}
double output = 0.0;
for (int i=start_index; i<=end_index; ++i)
{
output += pow(data[i]-mean, 2);
}
return sqrt(output/n);
}
double BasicStatistics::median(const QVector<double>& data, bool check_sorted)
{
if (check_sorted && !isSorted(data))
{
THROW(StatisticsException, "Cannot calculate median on unsorted data array.");
}
const int n = data.count();
if (n==0)
{
THROW(StatisticsException, "Cannot calculate median on empty data array!");
}
if (n%2==0)
{
return 0.5 * (data[n/2] + data[n/2-1]);
}
else
{
return data[n/2];
}
}
double BasicStatistics::mad(const QVector<double>& data, double median)
{
QVector<double> devs;
devs.reserve(data.count());
foreach(double value, data)
{
devs.append(fabs(value-median));
}
std::sort(devs.begin(), devs.end());
return BasicStatistics::median(devs, false);
}
double BasicStatistics::q1(const QVector<double>& data, bool check_sorted)
{
if (check_sorted && !isSorted(data))
{
THROW(StatisticsException, "Cannot calculate q1 on unsorted data array.");
}
const int n = data.count();
if (n==0)
{
THROW(StatisticsException, "Cannot calculate q1 on empty data array!");
}
return data[n/4];
}
double BasicStatistics::q3(const QVector<double>& data, bool check_sorted)
{
if (check_sorted && !isSorted(data))
{
THROW(StatisticsException, "Cannot calculate q3 on unsorted data array.");
}
const int n = data.count();
if (n==0)
{
THROW(StatisticsException, "Cannot calculate q3 on empty data array!");
}
return data[3*n/4];
}
double BasicStatistics::correlation(const QVector<double>& x, const QVector<double>& y)
{
return correlation(x, y, 0, x.size()-1);
}
double BasicStatistics::correlation(const QVector<double>& x, const QVector<double>& y, int start_index, int end_index)
{
if (x.count()!=y.count())
{
THROW(StatisticsException, "Cannot calculate correlation of data arrays with different length!");
}
if (x.count()==0)
{
THROW(StatisticsException, "Cannot calculate correlation of data arrays with zero length!");
}
const double x_mean = mean(x, start_index, end_index);
const double y_mean = mean(y, start_index, end_index);
double sum = 0.0;
for(int i=start_index; i<=end_index; ++i)
{
sum += (x[i]-x_mean) * (y[i]-y_mean);
}
return sum / stdev(x, x_mean, start_index, end_index) / stdev(y, y_mean, start_index, end_index) / (end_index - start_index + 1);
}
bool BasicStatistics::isValidFloat(double value)
{
if (std::isnan(value))
{
return false;
}
if (value > std::numeric_limits<double>::max())
{
return false;
}
if (value < -std::numeric_limits<double>::max())
{
return false;
}
return true;
}
bool BasicStatistics::isValidFloat(QByteArray value)
{
bool ok = true;
double numeric_value = value.toDouble(&ok);
return ok && isValidFloat(numeric_value);
}
bool BasicStatistics::isSorted(const QVector<double>& data)
{
for (int i=1; i<data.count(); ++i)
{
if (data[i-1]>data[i]) return false;
}
return true;
}
int BasicStatistics::sign(int val)
{
return (0<val) - (val<0);
}
QPair<double, double> BasicStatistics::linearRegression(const QVector<double>& x, const QVector<double>& y)
{
// initializing sum of x and y values
int count_valid = 0;
double sum_x = 0.0;
double sum_y = 0.0;
for (int i=0; i<x.size(); ++i)
{
if (isValidFloat(x[i]) && isValidFloat(y[i]))
{
sum_x += x[i];
sum_y += y[i];
++count_valid;
}
}
// middle index of the section
double sxoss = sum_x / count_valid;
// initializing b
double slope = 0.0;
double st2 = 0.0;
for (int i=0; i<x.size(); ++i)
{
if (isValidFloat(x[i]) && isValidFloat(y[i]))
{
// t is distance from the middle index
double t = x[i]-sxoss;
// sum of the squares of the distance from the average
st2 += t*t;
// b is sum of datapoints weighted by the distance
slope += t * y[i];
}
}
// averaging b by the maximum distance from the average
slope /= st2;
// average difference between data points and distance weighted data points.
double offset = (sum_y - sum_x*slope) / count_valid;
//create output
return qMakePair(offset, slope);
}
QPair<double, double> BasicStatistics::getMinMax(const QVector<double>& data)
{
double min = std::numeric_limits<double>::max();
double max = -std::numeric_limits<double>::max();
foreach(double v, data)
{
if (!isValidFloat(v)) continue;
min = std::min(min, v);
max = std::max(max, v);
}
return qMakePair(min, max);
}
void BasicStatistics::precalculateFactorials()
{
if (!factorial_cache.isEmpty()) return;
//calculate factorials until double overflow happens
int i = 0;
double value = 1.0;
while(isValidFloat(value))
{
factorial_cache.append(value);
++i;
value *= i;
}
}
double BasicStatistics::factorial(int n)
{
//outside of valid ragen => exception
if (n<0 || factorial_cache.count()==0)
{
THROW(ProgrammingException, "Cannot calculate factorial of " + QByteArray::number(n) + "! Cache not initialized?");
}
//not in cache (i.e. double overflow) => NAN
if (factorial_cache.count()<n+1)
{
return std::numeric_limits<double>::quiet_NaN();
}
return factorial_cache[n];
}
double BasicStatistics::matchProbability(double p, int n, int count)
{
//handle double overflow of factorial (approximately at 160)
int mismatches = count - n;
while(!BasicStatistics::isValidFloat(BasicStatistics::factorial(count)))
{
n /= 2;
mismatches /=2;
count = n + mismatches;
}
//calculate probability
double output = 0.0;
for (int i=n; i<=count; ++i)
{
double q = std::pow(1.0-p, count-i) * std::pow(p, i) * BasicStatistics::factorial(count) / BasicStatistics::factorial(i) / BasicStatistics::factorial(count-i);
output += q;
}
//check that result is valid
if (!BasicStatistics::isValidFloat(output))
{
THROW(ProgrammingException, "Calculated probabilty for " + QString::number(n) + " matches and " + QString::number(mismatches) + " mismatches is not a valid float!");
}
return output;
}
void BasicStatistics::precalculateLogFactorials()
{
if (!log_factorial_cache.isEmpty()) return;
//calculate factorials until double overflow happens
int i = 0;
double value = 0.0;
while(isValidFloat(value) && (i < 100000))
{
log_factorial_cache.append(value);
++i;
value += log(i);
}
}
double BasicStatistics::logFactorial(int n)
{
if (n<0) THROW(ProgrammingException, "Cannot calculate log factorial of negative number " + QByteArray::number(n) + "!");
if(log_factorial_cache.count()==0) THROW(ProgrammingException, "Cannot calculate log factorial! Cache not initialized!");
if(n >= LOG_FACTORIAL_CACHE_SIZE)
{
THROW(ProgrammingException, "Cannot calculate log factorial of " + QByteArray::number(n) + "! Number exceeds cache size of " + QByteArray::number(LOG_FACTORIAL_CACHE_SIZE) + "!");
}
return log_factorial_cache[n];
}
double BasicStatistics::hypergeometricLogProbability(int a, int b, int c, int d)
{
return logFactorial(a+b) + logFactorial(c+d) + logFactorial(a+c) + logFactorial(b+d) - logFactorial(a) - logFactorial(b) - logFactorial(c) - logFactorial(d) - logFactorial(a+b+c+d);
}
double BasicStatistics::fishersExactTest(int a, int b, int c, int d, QByteArray type)
{
// check input
QList<QByteArray> valid_types;
valid_types << "two-sided" << "greater" << "less";
if(!valid_types.contains(type))
{
THROW(ArgumentException, "Invalid type '" + type + "' provided! Valid types are: '" + valid_types.join("', '") + "' ");
}
if(a<0 || b<0 || c<0 || d<0)
{
THROW(ArgumentException, "Cannot perform Fisher's Exact Test on negative counts!");
}
precalculateLogFactorials();
int n = a + b + c + d;
double log_p_cutoff = hypergeometricLogProbability(a, b, c, d) + 1e-12; //add small offset to ensure '<=' operation below works even with double precision
double p_fraction = 0.0;
for (int i = 0; i <= n; ++i)
{
if ((a+b-i >= 0) && (a+c-i >= 0) && (d-a+i >= 0))
{
double log_p = hypergeometricLogProbability(i, a+b-i, a+c-i, d-a+i);
if ((type == "two-sided") && (log_p <= log_p_cutoff))
{
p_fraction += exp(log_p - log_p_cutoff);
}
else if((type == "greater") && (i >= a))
{
p_fraction += exp(log_p - log_p_cutoff);
}
else if((type == "less") && (i <= a))
{
p_fraction += exp(log_p - log_p_cutoff);
}
}
}
return std::min(1.0, exp(log_p_cutoff + log(p_fraction)));
}