Skip to content

Commit

Permalink
Improve accuracy of kde() invcdf estimates (gh-124637)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger authored Sep 27, 2024
1 parent 26a7420 commit 4b89c5e
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,12 @@ def f_inv(y):
return f_inv

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

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

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

@register('triweight')
Expand Down

0 comments on commit 4b89c5e

Please sign in to comment.