Repository navigation
Expand file tree
/
Copy pathStatistics.cpp
More file actions
152 lines (127 loc) · 2.92 KB
/
Copy pathStatistics.cpp
File metadata and controls
152 lines (127 loc) · 2.92 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
#include <cmath>
#include "Statistics.h"
double Statistics::leftTailedFisher( int a, int b, int c, int d)
{
int n = a + b + c + d;
double p = 0;
int min;
// Calculate the p-value for the initial table
p = Statistics::hypergeometricProbability( a, b, c, d);
if( a < d)
{
min = a;
}
else
{
min = d;
}
// For every other valid table, calculate the p-value for that table and add it to the original p-value
for( int i = 0; i < min; i++)
{
a = a - 1;
d = d - 1;
b = b + 1;
c = c + 1;
double pNew = Statistics::hypergeometricProbability( a, b, c, d);
p = p + pNew;
}
return p;
}
double Statistics::hypergeometricProbability( int a, int b, int c, int d)
{
int n = a + b + c + d;
double p = 1.0;
int j = n;
for( int i = a + 1; i <= a + c; i++)
{
p = p * i;
if( p > 1)
{
p = p / ( double) j;
j--;
}
}
for( int i = c + 1; i <= c + d; i++)
{
p = p * i;
if( p > 1)
{
p = p / ( double) j;
j--;
}
}
for( int i = b + 1; i <= b + d; i++)
{
p = p * i;
if( p > 1)
{
p = p / ( double) j;
j--;
}
}
while( j > a + b + 1)
{
p = p / ( double) j;
j--;
}
for( int i = 1; i <= c; i++)
{
p = p / ( double) i;
}
return p;
}
// Smart idea to compute the Poisson PDF without actually calculating
// k! directly. We use gamma function instead and solve the PDF in
// logarithmic domain.
// The idea and code snippet for Statistics::poissonPDF by Markus Saers:
// www.masaers.com/2013/10/08/Implementing-Poisson-pmf.html
double Statistics::poissonPDF(const double k, const double lambda)
{
return std::exp(k * std::log(lambda) - std::lgamma(k + 1.0) - lambda);
}
// Now using the efficient implementation of the Poisson PDF above,
// we can easily compute the Poisson CDF without having to rely on
// any external statistical libraries.
double Statistics::poissonCDF(const double k, const double lambda)
{
double sum = 0;
for(int i = 0; i <= std::floor(k); i++)
{
sum = sum + Statistics::poissonPDF((double) i, lambda);
}
return sum;
}
double Statistics::mean( const std::vector<double> values)
{
double sum = 0;
int numberOfElements = values.size();
for( int i = 0; i < numberOfElements; i++)
{
sum = sum + values[i];
}
double mean = ( double) sum / ( double) numberOfElements;
return mean;
}
double Statistics::variance( const std::vector<double> values, const double mean)
{
double sum = 0;
int numberOfElements = values.size();
for( int i = 0; i < numberOfElements; i++)
{
double difference = values[i] - mean;
double squaredDifference = pow( difference, 2.0);
sum = sum + squaredDifference;
}
double variance = ( double) sum / ( double) numberOfElements;
return variance;
}
double Statistics::standardDeviation( const double variance)
{
double standardDeviation = sqrt( variance);
return standardDeviation;
}
double Statistics::coefficientOfVariation( const double mean, const double standardDeviation)
{
double cov = standardDeviation / mean;
return cov;
}