forked from ding-lab/msisensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chi.cpp
156 lines (130 loc) · 4.16 KB
/
chi.cpp
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
/*
*
* Calculate the Chi-Squared P-Value
* The functions: approx_gamma, igf and chisqr were from:
* http://www.codeproject.com/Articles/432194/How-to-Calculate-the-Chi-Squared-P-Value
* under the permission:
* http://creativecommons.org/licenses/publicdomain
*
*
*/
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cmath>
#include <assert.h>
#include <unistd.h>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
/*
double gamma(double N) {
const long double SQRT2PI = 2.5066282746310005024157652848110452530069867406099383;
long double Z = (long double)N;
long double Sc = powl((Z + A), (Z + 0.5));
Sc *= expl(-1.0 * (Z + A));
Sc /= Z;
long double F = 1.0;
long double Ck;
long double Sum = SQRT2PI;
for(int K = 1; K < A; K++) {
Z++;
Ck = powl(A - K, K - 0.5);
Ck *= expl(A - K);
Ck /= F;
Sum += (Ck / Z);
F *= (-1.0 * K);
}
return (double)(Sum * Sc);
}
*/
double approx_gamma(double Z) {
const double RECIP_E = 0.36787944117144232159552377016147; // RECIP_E = (E^-1) = (1.0 / E)
const double TWOPI = 6.283185307179586476925286766559; // TWOPI = 2.0 * PI
double D = 1.0 / (10.0 * Z);
D = 1.0 / ((12 * Z) - D);
D = (D + Z) * RECIP_E;
D = pow(D, Z);
D *= sqrt(TWOPI / Z);
return D;
}
static double igf(double S, double Z) {
if (Z < 0.0) { return 0.0; }
double Sc = (1.0 / S);
Sc *= pow(Z, S);
Sc *= exp(-Z);
double Sum, Nom, Denom;
Sum = Nom = Denom = 1.0;
for(int I = 0; I < 200; I++) {
Nom *= Z;
S++;
Denom *= S;
Sum += (Nom / Denom);
}
return Sum * Sc;
}
double chisqr(int Dof, double Cv) {
if (Cv < 0 || Dof < 1) { return 0.0; }
double K = ((double)Dof) * 0.5;
double X = Cv * 0.5;
if (Dof == 2) { return exp(-1.0 * X); }
double PValue = igf(K, X);
if (std::isnan(PValue) || std::isinf(PValue) || PValue <= 1e-8) { return 1e-14; }
PValue /= approx_gamma(K);
return (1.0 - PValue);
}
double get_chisqr_p(unsigned short * first, unsigned short * second) {
double CriticalValue = 0.0;
double XSqr;
unsigned int NumberOfEffectiveDatapoints = 0;
for (unsigned index = 0; index < 100; index++) {
if (first[index] + second[index] > 0.0) NumberOfEffectiveDatapoints++;
XSqr = first[index] - second[index];
if (first[index] > 0.0) CriticalValue += (XSqr * XSqr) / first[index];
}
if (NumberOfEffectiveDatapoints < 2) NumberOfEffectiveDatapoints = 2;
double returnP = chisqr(NumberOfEffectiveDatapoints, CriticalValue);
std::cerr << CriticalValue << "\t" << NumberOfEffectiveDatapoints << "\t" << returnP << std::endl;
return returnP;
}
double X2BetweenTwo(unsigned short * FirstOriginal, unsigned short * SecondOriginal, unsigned int dispots) {
// default dispots = 100
// delare
double *ExpFirst, *ExpSecond, *SumBoth;
ExpFirst = new double [dispots + 1];
ExpSecond = new double [dispots + 1];
SumBoth = new double [dispots + 1];
double SumFirst, SumSecond, SumTotal;
SumFirst = SumSecond = SumTotal = 0.0;
// calculation
for (unsigned i = 0; i < dispots; i++) {
SumBoth[i] = FirstOriginal[i] + SecondOriginal[i];
SumFirst += FirstOriginal[i];
SumSecond += SecondOriginal[i];
}
SumTotal = SumFirst + SumSecond;
for (unsigned i = 0; i < dispots; i++) {
ExpFirst[i] = SumBoth[i] * SumFirst / SumTotal;
ExpSecond[i] = SumBoth[i] * SumSecond / SumTotal;
}
double result = 0.0;
unsigned Degree = 0;
for (unsigned i = 0; i < dispots; i++) {
if (FirstOriginal[i] + SecondOriginal[i] > 0.0) Degree++;
if (ExpFirst[i]) result += (FirstOriginal[i] - ExpFirst[i]) * (FirstOriginal[i] - ExpFirst[i]) / ExpFirst[i];
if (ExpSecond[i]) result += (SecondOriginal[i] - ExpSecond[i]) * (SecondOriginal[i] - ExpSecond[i]) / ExpSecond[i];
}
// release mem
double PValue;
if (Degree == 1 || result == 0) PValue = 1.0;
else {
Degree--;
PValue = chisqr(Degree, result);
}
if ( PValue < 0 ) PValue = PValue * (-1);
delete [] ExpFirst;
delete [] ExpSecond;
delete [] SumBoth;
return PValue;
}