-
Notifications
You must be signed in to change notification settings - Fork 4
/
beta_functions.py
191 lines (166 loc) · 5.66 KB
/
beta_functions.py
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
#!/usr/bin/env python
import math
from mpmath import *
PRECISION = 300
# Continued Fraction Computation
# 6.5.31 Handbook of Mathematical Functions, page 263
# Recursive implementation
def ibeta_cf(d,a,b,x):
if d == 100:
return mpf('0.0') # end at 100 iterations
if d == 0: # First term 1/1+|
mult = ((x**a)*((mpf('1.0')-x)**b))/a
mult = mult * gamma(a+b)
mult = mult / (gamma(a) * gamma(b))
m=0
return mult*mpf('1.0')/(mpf('1.0')+ibeta_cf(d+1,a,b,x))
elif ((d % 2) == 1):
m = (d-1)/2
result = (a+m)*(a+b+m)*x
result = -result/((a+(2*m))*(a+(2*m)+mpf('1.0')))
return result/(mpf('1.0')+ibeta_cf(d+1,a,b,x))
#return d2mp1(a,b,m,x)/(mpf('1.0')+ibeta_cf(d+1,a,b,x))
else:
m = d/2
result = (m*(b-m)*x)/((a+(2*m)-mpf('1.0'))*(a+(2*m)))
return result/(mpf('1.0')+ibeta_cf(d+1,a,b,x))
#return d2m(a,b,m,x)/(mpf('1.0')+ibeta_cf(d+1,a,b,x))
# An iterative version working backwards through the continued fraction
def ibeta_cf_backwards(a,b,x):
f = mpf('0.0') # running fraction value
for d in range(100,-1,-1):
if d == 0: # First Term (last of iteration) 1/1+|
mult = ((x**a)*((mpf('1.0')-x)**b))/a
mult = mult * gamma(a+b)
mult = mult / (gamma(a) * gamma(b))
m=0
return mult*mpf('1.0')/(mpf('1.0')+f)
elif ((d % 2) == 1): # Odd terms e_{2*m +1}
m = (d-1)/2
numerator = (a+m)*(a+b+m)*x
numerator = -numerator/((a+(2*m))*(a+(2*m)+mpf('1.0')))
f = numerator/(mpf('1.0')+f)
else: # Even terms e_{2*m}
m = d/2
numerator = m*(b-m)*x
numerator = numerator/((a+(2*m)-mpf('1.0'))*(a+(2*m)))
f = numerator/(mpf('1.0')+f)
# An iterative version working backwards, using equations from 26.5.9.
def ibeta_cf_backwards2(a,b,x):
f = mpf('0.0') # running fraction value
for e in range(100,0,-1):
if e == 1: # First Term (last of iteration) 1/1+|
mult = ((x**a)*((mpf('1.0')-x)**(b-mpf('1.0'))))/a
mult = mult * gamma(a+b)
mult = mult / (gamma(a) * gamma(b))
m=0
return mult*mpf('1.0')/(mpf('1.0')+f)
elif ((e % 2) == 1): # Odd terms d_n/1+|
m = (e-1)/2
numerator = m*(a+b+m-mpf('1.0'))*x
numerator = numerator/((a+(2*m)-mpf('1.0'))* (a+(2*m)) * (mpf('1.0')-x))
f = numerator/(mpf('1.0')+f)
else: # Even terms d_{n+1}+|
m = e/2
numerator = (a+m-mpf('1.0'))*(b-m)*x
numerator = -numerator/((a+(2*m)-mpf('2.0'))*(a+(2*m)-mpf('1.0'))*(mpf('1.0')-x))
f = numerator/(mpf('1.0')+f)
def ibeta(a,b,x):
if (x == 0.0 or x==1.0):
return x
if x < ((a-1.0)/(a+b-2.0)):
return ibeta_cf(0,a,b,x)
else:
return mpf(1.0)-ibeta_cf(0,b,a,mpf('1.0')-x)
def ibeta_backwards(a,b,x):
if (x == 0.0 or x==1.0):
return x
if x < ((a-1.0)/(a+b-2.0)):
return ibeta_cf_backwards(a,b,x)
else:
return mpf(1.0)-ibeta_cf_backwards(b,a,mpf('1.0')-x)
def ibeta_backwards2(a,b,x):
if (x == 0.0 or x==1.0):
return x
if x < ((a-1.0)/(a+b-2.0)):
return ibeta_cf_backwards2(a,b,x)
else:
return mpf(1.0)-ibeta_cf_backwards2(b,a,mpf('1.0')-x)
# Binomial CDF
def BCDF(n, k, p):
return mpf('1.0') - ibeta(mpf(k+1),mpf(n-k),p)
# Binomial CDF
def BCDF_backwards(n, k, p):
return mpf('1.0') - ibeta_backwards(mpf(k+1),mpf(n-k),p)
# Binomial CDF
def BCDF_backwards2(n, k, p):
return mpf('1.0') - ibeta_backwards2(mpf(k+1),mpf(n-k),p)
# Find smallest k where B(n,k,p) > alpha
# using binary chop search
# Equivalent to Excel CRITBINOM function
def binomial_quantile(n, p, alpha):
min = 0;
max = n;
mid = min + ((max-min) >> 1)
keepgoing = True
while (keepgoing):
b = BCDF(n,mid,p)
if (b > alpha):
max = mid
elif (b < alpha):
min = mid
elif (b == alpha):
keepgoing = False
newmid = min + ((max-min) >> 1)
if (newmid == mid):
keepgoing=False
mid = newmid
if (b < alpha): # Make sure we have smallest b > alpha
mid += 1
return mid
# Find smallest k where B(n,k,p) > alpha
# using binary chop search
# Equivalent to Excel CRITBINOM function
def binomial_quantile_backwards(n, p, alpha):
min = 0;
max = n;
mid = min + ((max-min) >> 1)
keepgoing = True
while (keepgoing):
b = BCDF_backwards(n,mid,p)
if (b > alpha):
max = mid
elif (b < alpha):
min = mid
elif (b == alpha):
keepgoing = False
newmid = min + ((max-min) >> 1)
if (newmid == mid):
keepgoing=False
mid = newmid
if (b < alpha): # Make sure we have smallest b > alpha
mid += 1
return mid
# Find smallest k where B(n,k,p) > alpha
# using binary chop search
# Equivalent to Excel CRITBINOM function
def binomial_quantile_backwards2(n, p, alpha):
min = 0;
max = n;
mid = min + ((max-min) >> 1)
keepgoing = True
while (keepgoing):
b = BCDF_backwards2(n,mid,p)
if (b > alpha):
max = mid
elif (b < alpha):
min = mid
elif (b == alpha):
keepgoing = False
newmid = min + ((max-min) >> 1)
if (newmid == mid):
keepgoing=False
mid = newmid
if (b < alpha): # Make sure we have smallest b > alpha
mid += 1
return mid