Skip to content

Commit

Permalink
ENH: stats.kde.resample can use np.random.Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfaff committed Jan 26, 2020
1 parent 9af5946 commit 397f64b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
16 changes: 9 additions & 7 deletions scipy/stats/kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,15 @@ def resample(self, size=None, seed=None):
The number of samples to draw. If not provided, then the size is
the same as the effective number of samples in the underlying
dataset.
seed : None or int or `np.random.RandomState`, optional
If `seed` is None, random variates are drawn by the RandomState
singleton used by np.random.
If `seed` is an int, a new `np.random.RandomState` instance is used,
seeded with seed.
If `seed` is already a `np.random.RandomState instance`, then that
`np.random.RandomState` instance is used.
seed : {None, int, `~np.random.RandomState`, `~np.random.Generator`}, optional
This parameter defines the object to use for drawing random
variates.
If `seed` is `None` the `~np.random.RandomState` singleton is used.
If `seed` is an int, a new ``RandomState`` instance is used, seeded
with seed.
If `seed` is already a ``RandomState`` or ``Generator`` instance,
then that object is used.
Default is None.
Specify `seed` for reproducible drawing of random variates.
Returns
Expand Down
9 changes: 9 additions & 0 deletions scipy/stats/tests/test_kdeoth.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ def test_seed_sub(gkde_trail):
samp2 = gkde_trail.resample(n_sample, seed=rstate2)
assert_allclose(samp1, samp2, atol=1e-13)

# check that np.random.Generator can be used (numpy >= 1.17)
try:
# obtain a np.random.Generator object
rng = np.random.default_rng(1234)
gkde_trail.resample(n_sample, seed=rng)
except AttributeError:
# only available in numpy >= 1.17
pass

np.random.seed(8765678)
n_basesample = 500
wn = np.random.rand(n_basesample)
Expand Down

0 comments on commit 397f64b

Please sign in to comment.