diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2024-09-27 16:56:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-27 16:56:37 (GMT) |
commit | 4b89c5ebfc7d5d4f008eee0ae6da765dfc28e3a9 (patch) | |
tree | 30a139ee6604a88f36c985c977c6c2e69d1b6e12 /Lib | |
parent | 26a74203f08c59922b4889931c108b34c4ee8e72 (diff) | |
download | cpython-4b89c5ebfc7d5d4f008eee0ae6da765dfc28e3a9.zip cpython-4b89c5ebfc7d5d4f008eee0ae6da765dfc28e3a9.tar.gz cpython-4b89c5ebfc7d5d4f008eee0ae6da765dfc28e3a9.tar.bz2 |
Improve accuracy of kde() invcdf estimates (gh-124637)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/statistics.py | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py index d3dd0d5..f193fcdc 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -870,9 +870,12 @@ def _newton_raphson(f_inv_estimate, f, f_prime, tolerance=1e-12): 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 @@ -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') |