8000 Add statistics recipe for sampling from an estimated probability density distribution by rhettinger · Pull Request #117221 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Add statistics recipe for sampling from an estimated probability density distribution #117221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add invcdf() / cdf() round trip tests
  • Loading branch information
rhettinger committed Mar 26, 2024
commit 211836c9369447ff7729cf941dca49b501a76b86
8000
17 changes: 15 additions & 2 deletions Doc/library/statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1163,11 +1163,12 @@ cumulative distribution functions.
.. testcode::

from random import choice, random, seed
from math import log, tan, sqrt, asin
from math import sqrt, log, pi, tan, asin
from statistics import NormalDist

kernel_invcdfs = {
'normal': NormalDist().inv_cdf,
'logisitic': lambda p: log(p / (1 - p)),
'logistic': lambda p: log(p / (1 - p)),
'sigmoid': lambda p: log(tan(p * pi/2)),
'rectangular': lambda p: 2*p - 1,
'triangular': lambda p: sqrt(2*p) - 1 if p < 0.5 else 1 - sqrt(2 - 2*p),
Expand All @@ -1191,6 +1192,18 @@ For example:
>>> [round(rand(), 1) for i in range(10)]
[0.7, 6.2, 1.2, 6.9, 7.0, 1.8, 2.5, -0.5, -1.8, 5.6]

.. testcode::
:hide:

from statistics import kde
from math import isclose

# Verify that cdf / invcdf will round trip
xarr = [i/100 for i in range(-100, 101)]
for kernel, invcdf in kernel_invcdfs.items():
cdf = kde([0], h=1.0, kernel=kernel, cumulative=True)
for x in xarr:
assert isclose(invcdf(cdf(x)), x, abs_tol=1E-9)

..
# This modelines must appear within the last ten lines of the file.
Expand Down
0