11from __future__ import print_function
2- from math import log , exp
2+ from math import log
3+
34
45def test_kernels (bits , sample , radius ):
56 import sdm as sdmlib
@@ -15,6 +16,7 @@ def test_kernels(bits, sample, radius):
1516 valid_kernels .append (kernel )
1617 return valid_kernels
1718
19+
1820def div_pow2 (x , a ):
1921 try :
2022 r = x
@@ -24,6 +26,7 @@ def div_pow2(x, a):
2426 except OverflowError :
2527 return 2 ** (log (x )/ log (2 ) - a )
2628
29+
2730def calculate_probabilities (bits ):
2831 from math import factorial
2932 comb = lambda a , b : factorial (a )// factorial (b )// factorial (a - b )
@@ -32,25 +35,29 @@ def calculate_probabilities(bits):
3235 acc .append (acc [- 1 ] + comb (bits , i ))
3336 return [div_pow2 (x , bits ) for x in acc [1 :]]
3437
38+
3539def __calculate_radius (bits , threshold = 0.001 ):
3640 from math import factorial
3741 comb = lambda a , b : factorial (a )// factorial (b )// factorial (a - b )
3842 x = 0
3943 for i in range (bits + 1 ):
4044 x += comb (bits , i )
4145 p = div_pow2 (x , bits )
42- if p >= threadhold :
46+ if p >= threshold :
4347 break
4448 return i
4549
50+
4651def calculate_radius (bits , threshold = 0.001 ):
4752 from scipy .stats import norm
4853 return int (bits / 2.0 + norm .ppf (threshold )* (bits ** 0.5 )/ 2.0 )
49- if threadhold == 0.001 :
54+
55+ if threshold == 0.001 :
5056 # norminv(0.001) = 3.090232306167814
5157 return int (bits / 2.0 - 3.090232306167814 * (bits ** 0.5 )/ 2.0 )
5258 return __calculate_radius (bits , threshold )
5359
60+
5461def calculate_probability (bits , radius ):
5562 from scipy .stats import binom
5663 return binom .cdf (radius , bits , 0.5 )
0 commit comments