8000 Improve accuracy of kde() invcdf estimates (gh-124637) · python/cpython@4b89c5e · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b89c5e

Browse files
authored
Improve accuracy of kde() invcdf estimates (gh-124637)
1 parent 26a7420 commit 4b89c5e

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Lib/statistics.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,12 @@ def f_inv(y):
870870
return f_inv
871871

872872
def _quartic_invcdf_estimate(p):
873+
# A handrolled piecewise approximation. There is no magic here.
873874
sign, p = (1.0, p) if p <= 1/2 else (-1.0, 1.0 - p)
875+
if p < 0.0106:
876+
return ((2.0 * p) ** 0.3838 - 1.0) * sign
874877
x = (2.0 * p) ** 0.4258865685331 - 1.0
875-
if p >= 0.004 < 0.499:
878+
if p < 0.499:
876879
x += 0.026818732 * sin(7.101753784 * p + 2.73230839482953)
877880
return x * sign
878881

@@ -886,8 +889,11 @@ def quartic_kernel():
886889
return pdf, cdf, invcdf, support
887890

888891
def _triweight_invcdf_estimate(p):
892+
# A handrolled piecewise approximation. There is no magic here.
889893
sign, p = (1.0, p) if p <= 1/2 else (-1.0, 1.0 - p)
890894
x = (2.0 * p) ** 0.3400218741872791 - 1.0
895+
if 0.00001 < p < 0.499:
896+
x -= 0.033 * sin(1.07 * tau * (p - 0.035))
891897
return x * sign
892898

893899
@register('triweight')

0 commit comments

Comments
 (0)
0