Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
49f3471
Add kde_rand()
rhettinger Apr 5, 2024
0fd40f3
Update __all__
rhettinger Apr 5, 2024
75680e5
Auto detect size changes
rhettinger Apr 6, 2024
63ba396
.
rhettinger Apr 6, 2024
c69c504
Factor-out s-scurve function
rhettinger Apr 6, 2024
dcd83f2
.
rhettinger Apr 6, 2024
57492bf
.
rhettinger Apr 6, 2024
5b41598
.
rhettinger Apr 7, 2024
89a6033
Add docs for kde_random().
rhettinger Apr 7, 2024
4d4f792
.
rhettinger Apr 7, 2024
7848cca
.
rhettinger Apr 7, 2024
37a8955
Improve docstrings
rhettinger Apr 7, 2024
a30fb9d
The "with locks" comment was inaccurate.
rhettinger Apr 7, 2024
a2a077f
Add summary table entry. Fix doctests.
rhettinger Apr 7, 2024
a82b100
Fix typo
rhettinger Apr 8, 2024
1397e91
.
rhettinger Apr 11, 2024
630d942
Change variable name to match the supporting reference.
rhettinger Apr 13, 2024
e9e5d54
Closed-form for _parabolic_invcdf().
rhettinger Apr 15, 2024
8a0fd69
Flip sign in Newton-Raphson to match the common presentation.
rhettinger Apr 15, 2024
879a1c8
Merge branch 'main' into kde_rand
rhettinger Apr 15, 2024
3459bf2
Add kernel_invcdf tests
rhettinger Apr 17, 2024
1ca870a
Better _quartic_invcdf_estimate
rhettinger Apr 19, 2024
96a5f14
Make standalone _triweight_invcdf_estimate()
rhettinger Apr 19, 2024
f05eddb
Floats everywhere
rhettinger Apr 19, 2024
065be11
.
rhettinger Apr 23, 2024
3ac42df
Merge branch 'main' into kde_rand
rhettinger Apr 24, 2024
bba0bdf
Merge branch 'main' into kde_rand
rhettinger Apr 30, 2024
d71ca9d
Never used the global, shared Random instance.
rhettinger Apr 30, 2024
ee43ed7
.
rhettinger Apr 30, 2024
7ed7d41
Merge branch 'main' into kde_rand
rhettinger Apr 30, 2024
f85c303
Update whatsnew
rhettinger Apr 30, 2024
544ca8f
Expand "it" variable name to "iterator"
rhettinger Apr 30, 2024
1249d14
Test the kde_random() outer function
rhettinger May 4, 2024
258e0f4
Merge branch 'main' into kde_rand
rhettinger May 4, 2024
a09d30b
Add fully qualified reference
rhettinger May 4, 2024
405d443
Add an approximate distribution test
rhettinger May 4, 2024
a4692bf
Use F_hat() for a better estimate
rhettinger May 4, 2024
ec2d9f2
Test the curve in more places
rhettinger May 4, 2024
c612c1f
Refine the reproducibility note.
rhettinger May 4, 2024
1324952
Missing CDF qualifier
rhettinger May 4, 2024
13e702f
Word smithing
rhettinger May 4, 2024
15a4764
Word smithing
rhettinger May 4, 2024
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
Improve docstrings
  • Loading branch information
rhettinger committed Apr 7, 2024
commit 37a89553ae953ee4598c3dbb4feffabd8654d045
7 changes: 5 additions & 2 deletions Doc/library/statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,11 @@ However, for reading convenience, most of the examples show sorted sequences.
Return a function that makes a random selection from the estimated
probability density function produced by ``kde(data, h, kernel)``.

For reproducible results, set *seed* to an integer, float, str, or bytes.
Not thread-safe without a lock around calls.
Providing a *seed* allows reproducible selections within a single
thread (or with a lock around calls). In the future, the selection
method for the *parabolic*, *quartic*, and *triweight* kernels may be
replaced with faster algorithms that give different results. The
seed may be an integer, float, str, or bytes.

A :exc:`StatisticsError` will be raised if the *data* sequence is empty.

Expand Down
21 changes: 16 additions & 5 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@ def f_inv(y):

def _simple_s_curve(power):
"S-curve that crosses (0, -1), (1/2, 0), (1, 1)."
# Approximates the invcdf for kernels with support: -1 <= x <= 1.
# Approximates the inverse CDF for kernels with support: -1 < x < 1.
return lambda p: ((2 * p) ** power - 1
if p <= 1/2 else
1 - (2 - 2*p) ** power)
Expand Down Expand Up @@ -1762,12 +1762,23 @@ def _simple_s_curve(power):

def kde_random(data, h, kernel='normal', *, seed=None):
"""Return a function that makes a random selection from the estimated
probability density function created by: kde(data, h, kernel)
probability density function created by kde(data, h, kernel).

For reproducible results, set *seed* to an integer, float, str, or bytes.
Not thread-safe without a lock around calls.
Providing a *seed* allows reproducible selections within a single
thread (or with a lock around calls). In the future, the selection
method for the parabolic, quartic, and triweight kernels may be
replaced with faster algorithms that give different results.
The seed may be an integer, float, str, or bytes.

A StatisticsError will be raised if the data sequence is empty.
A StatisticsError will be raised if the *data* sequence is empty.

Example:

>>> data = [-2.1, -1.3, -0.4, 1.9, 5.1, 6.2]
>>> rand = kde_random(sample, h=1.5, seed=8675309)
>>> new_selections = [rand() for i in range(10)]
>>> [round(x, 1) for x in new_selections]
[0.7, 6.2, 1.2, 6.9, 7.0, 1.8, 2.5, -0.5, -1.8, 5.6]

"""
n = len(data)
Expand Down