-
Notifications
You must be signed in to change notification settings - Fork 0
/
binom.c
189 lines (184 loc) · 4.65 KB
/
binom.c
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
#include <stdint.h>
#include <inttypes.h>
// for INFINITY
#include <math.h>
#define EPSILON 0.0000000000001
// A bias of this sort is needed to enable calculation of p-values down to the
// minimum representable positive number.
#define EXACT_TEST_BIAS 0.00000000000000000000000010339757656912845935892608650874535669572651386260986328125
double binom_2sided(uint32_t succ, uint32_t obs, double rate, uint32_t midp) {
// straightforward to generalize this to any success probability
double cur_succ_t2 = (int32_t)succ;
double cur_fail_t2 = (int32_t)(obs - succ);
double tailp = (1 - EPSILON) * EXACT_TEST_BIAS;
double centerp = 0;
double lastp2 = tailp;
double lastp1 = tailp;
int32_t tie_ct = 1;
double rate_mult_incr = rate / (1 - rate);
double rate_mult_decr = (1 - rate) / rate;
double cur_succ_t1;
double cur_fail_t1;
double preaddp;
if (!obs) {
return midp? 0.5 : 1;
}
if (obs * rate < succ) {
while (cur_succ_t2 > 0.5) {
cur_fail_t2 += 1;
lastp2 *= rate_mult_decr * cur_succ_t2 / cur_fail_t2;
cur_succ_t2 -= 1;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * EPSILON) * EXACT_TEST_BIAS) {
tie_ct++;
}
tailp += lastp2;
break;
}
centerp += lastp2;
if (centerp == INFINITY) {
return 0;
}
}
if ((centerp == 0) && (!midp)) {
return 1;
}
while (cur_succ_t2 > 0.5) {
cur_fail_t2 += 1;
lastp2 *= rate_mult_decr * cur_succ_t2 / cur_fail_t2;
cur_succ_t2 -= 1;
preaddp = tailp;
tailp += lastp2;
if (tailp <= preaddp) {
break;
}
}
cur_succ_t1 = (int32_t)(succ + 1);
cur_fail_t1 = (int32_t)(obs - succ);
while (cur_fail_t1 > 0.5) {
lastp1 *= rate_mult_incr * cur_fail_t1 / cur_succ_t1;
preaddp = tailp;
tailp += lastp1;
if (tailp <= preaddp) {
break;
}
cur_succ_t1 += 1;
cur_fail_t1 -= 1;
}
} else {
while (cur_fail_t2 > 0.5) {
cur_succ_t2++;
lastp2 *= rate_mult_incr * cur_fail_t2 / cur_succ_t2;
cur_fail_t2--;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * EPSILON) * EXACT_TEST_BIAS) {
tie_ct++;
}
tailp += lastp2;
break;
}
centerp += lastp2;
if (centerp == INFINITY) {
return 0;
}
}
if ((centerp == 0) && (!midp)) {
return 1;
}
while (cur_fail_t2 > 0.5) {
cur_succ_t2 += 1;
lastp2 *= rate_mult_incr * cur_fail_t2 / cur_succ_t2;
cur_fail_t2 -= 1;
preaddp = tailp;
tailp += lastp2;
if (tailp <= preaddp) {
break;
}
}
cur_succ_t1 = (int32_t)succ;
cur_fail_t1 = (int32_t)(obs - succ);
while (cur_succ_t1 > 0.5) {
cur_fail_t1 += 1;
lastp1 *= rate_mult_decr * cur_succ_t1 / cur_fail_t1;
preaddp = tailp;
tailp += lastp1;
if (tailp <= preaddp) {
break;
}
cur_succ_t1 -= 1;
}
}
if (!midp) {
return tailp / (tailp + centerp);
} else {
return (tailp - ((1 - EPSILON) * EXACT_TEST_BIAS * 0.5) * tie_ct) / (tailp + centerp);
}
}
double binom_1sided(int32_t succ, int32_t obs, double rate) {
double cur_prob = EXACT_TEST_BIAS;
double left_prob = cur_prob;
double right_prob = 0;
double rate_mult_incr = rate / (1 - rate);
double rate_mult_decr = (1 - rate) / rate;
double cur_succ = succ;
double cur_fail = obs - succ;
double preaddp;
if (obs * rate < succ) {
while (cur_succ > 0.5) {
cur_fail += 1;
cur_prob *= rate_mult_decr * cur_succ / cur_fail;
cur_succ -= 1;
preaddp = left_prob;
left_prob += cur_prob;
if (left_prob <= preaddp) {
break;
}
if (left_prob >= 1.0) {
return 1;
}
}
cur_succ = succ;
cur_fail = obs - succ;
cur_prob = EXACT_TEST_BIAS;
right_prob = left_prob;
while (cur_fail > 0.5) {
cur_succ += 1;
cur_prob *= rate_mult_incr * cur_fail / cur_succ;
cur_fail -= 1;
preaddp = right_prob;
right_prob += cur_prob;
if (right_prob <= preaddp) {
break;
}
}
return left_prob / right_prob;
} else {
while (cur_fail > 0.5) {
cur_succ += 1;
cur_prob *= rate_mult_incr * cur_fail / cur_succ;
cur_fail -= 1;
preaddp = right_prob;
right_prob += cur_prob;
if (right_prob == INFINITY) {
return 0;
}
if (right_prob <= preaddp) {
break;
}
}
cur_succ = succ;
cur_fail = obs - succ;
cur_prob = EXACT_TEST_BIAS;
while (cur_succ > 0.5) {
cur_fail += 1;
cur_prob *= rate_mult_decr * cur_succ / cur_fail;
cur_succ -= 1;
preaddp = left_prob;
left_prob += cur_prob;
if (left_prob <= preaddp) {
break;
}
}
return left_prob / (left_prob + right_prob);
}
}