-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon_functions.py
executable file
·208 lines (187 loc) · 6.68 KB
/
common_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
from math import gamma,e
import sys
#import numpy as np
def eprint(*args,**kwargs):
print(*args,**kwargs, file=sys.stderr)
def vprint(verbose,*args,**kwargs):
if verbose:
print(*args,**kwargs, file=sys.stderr)
# Binary search
def pfunc(plocal,r,N):
q = 1.0-plocal
# Find x10
x = 0.0
for j in range(1,11):
x = 1.0 + (q*(plocal**r)*(x**(r+1.0)))
# do the equation
result = (1.0 - plocal*x)
result = result/((r+1.0 - (r*x))*q)
try:
result = result/(x**(N+1))
except OverflowError:
# catch OverflowError resulting from large N.
result = 0.0
return result
def search_for_p(r,N,iterations=1000, min_plocal=0.0, max_plocal=1.0, tolerance=0.00000001,verbose=False):
# Binary chop search for Plocal
iteration = 0
found = False
#vprint(verbose,"SEARCH FOR P")
#vprint(verbose,f'min {min_plocal} max {max_plocal} verbose={verbose} r={r} N={N}')
while (iteration < iterations):
candidate = (min_plocal + max_plocal)/2.0 # start in the middle of the range
result = pfunc(candidate,r,N)
#print ("iteration =",iteration)
#if verbose:
# vprint(verbose,f'candidate {candidate} min {min_plocal} max {max_plocal}')
iteration += 1
if iteration > iterations:
found = False
break
elif (result > (0.99-tolerance)) and (result < (0.99+tolerance)):
found = True
P_local = candidate
break
elif result > 0.99:
min_plocal = candidate
else:
max_plocal = candidate
if (found == False):
print ("Warning: P_local not found")
return P_local
# Binary search provided by Joshua Hill
## Binary search algorithm to find value of p s.t. the expected value
## of the Maurer Universal Statistic equals mu_bar within tolerance
## presumes a decreasing function
#def solve_for_p(mu_bar, n, v, tolerance=1e-09):
# assert n > 0
#
# #This is a hackish way of checking to see if the difference is within approximately 4 ULPs
# absEpsilon = 4.0 * max((np.nextafter(mu_bar, mu_bar+1.0) - mu_bar), (mu_bar - np.nextafter(mu_bar, mu_bar-1.0)))
# #If we don't have numpy, then this will work for most of the ranges we're concerned with
# #absEpsilon = 4.0*sys.float_info.epsilon
#
# if mu_bar > EppM(1.0/float(n), n, v):
# return False, 0.0
#
# ldomain = 1.0/float(n)
# hdomain = 1.0
#
# lbound = ldomain
# lvalue = float("inf")
# hbound = hdomain
# hvalue = float("-inf")
#
# #Note that the bounds are in the interval [0, 1], so underflows
# #are an issue, but overflows are not
# center = (lbound + hbound) / 2
# assert (center > ldomain) and (center < hdomain)
#
# centerVal = EppM(center, n, v)
#
# for rounds in range(1076):
# if isclose(mu_bar, centerVal, tolerance, absEpsilon):
# return True, center
#
# if mu_bar < centerVal:
# lbound = center
# lvalue = centerVal
# else:
# hbound = center
# hvalue = centerVal
##We now verify that ldomain <= lbound < center < hbound <= hdomain
##and that target in [ hvalue, lvalue ]
# if lbound >= hbound:
# print ("Bounds have converged after %d rounds and target was not found. Returning largest bound." % rounds)
# return True, min(max(lbound, hbound), hdomain)
#
# if (lbound < ldomain) or (lbound > hdomain) or (hbound < ldomain) or (hbound > hdomain):
# print ("The current search interval is not a subset of the domain after %d rounds and target was not found." % rounds)
# return False, 0.0
#
# if (mu_bar > lvalue) or (mu_bar < hvalue):
# print ("Target is not within the search interval after %d rounds" % rounds)
# return False, 0.0
#
# lastCenter = center
# center = (lbound + hbound) / 2.0
#
# if (center <= lbound) or (center >= hbound):
# print ("The next center is outside of the search interval after %d rounds" % rounds)
# return False, 0.0
#
# if lastCenter == center:
# print ("Detected cycle after %d rounds. Returning upper bound." % rounds)
# return True, hbound
#
# centerVal = EppM(center, n, v)
#
# #invariant: if this isn't true, then this isn't loosely monotonic
# if (centerVal < hvalue) or (centerVal > lvalue):
# print ("CenterVal is not within the search value interval after %d rounds. Returning upper bound." % rounds)
# return True, hbound
#
# #We ran out of rounds for the binary search
# if isclose(mu_bar, centerVal, tolerance, absEpsilon):
# return True, p
# else:
# print ("Ran out of search rounds. Returning upper bound")
# return True, min(hbound, hdomain)
# Continued Fraction Computation
# 6.5.31 Handbook of Mathematical Functions, page 263
# Recursive implementation
def upper_incomplete_gamma(a,x,d=0,iterations=100):
if d == iterations:
if ((d % 2) == 1):
return 1.0 # end iterations
else:
m = d/2
return x + (m-a)
if d == 0:
result = ((x**a) * (e**(-x)))/upper_incomplete_gamma(a,x,d=d+1)
return result
elif ((d % 2) == 1):
m = 1.0+((d-1.0)/2.0)
return x+ ((m-a)/(upper_incomplete_gamma(a,x,d=d+1)))
else:
m = d/2
return 1+(m/(upper_incomplete_gamma(a,x,d=d+1)))
# 6.5.31 Handbook of Mathematical Functions, page 263
# Recursive implementation
def upper_incomplete_gamma2(a,x,d=0,iterations=100):
if d == iterations:
return 1.0
if d == 0:
result = ((x**a) * (e**(-x)))/upper_incomplete_gamma2(a,x,d=d+1)
return result
else:
m = (d*2)-1
return (m-a)+x+ ((d*(a-d))/(upper_incomplete_gamma2(a,x,d=d+1)))
def lower_incomplete_gamma(a,x,d=0,iterations=100):
if d == iterations:
if ((d % 2) == 1):
return 1.0 # end iterations
else:
m = d/2
return x + (m-a)
if d == 0:
result = ((x**a) * (e**(-x)))/lower_incomplete_gamma(a,x,d=d+1)
return result
elif ((d % 2) == 1):
m = d - 1
n = (d-1.0)/2.0
return a + m - (((a+n)*x)/lower_incomplete_gamma(a,x,d=d+1))
else:
m = d-1
n = d/2.0
return a+m+((n*x)/(lower_incomplete_gamma(a,x,d=d+1)))
def lower_incomplete_gamma2(a,x):
return gamma(a)-upper_incomplete_gamma2(a,x)
def complimentary_incomplete_gamma(a,x):
return 1.0-upper_incomplete_gamma(a,x)
# Scipy name mappings
def gammainc(a,x):
return lower_incomplete_gamma(a,x)/gamma(a)
def gammaincc(a,x):
return upper_incomplete_gamma(a,x)/gamma(a)