13
13
from scipy .stats ._qmc import check_random_state
14
14
from scipy .stats ._resampling import BootstrapResult
15
15
from scipy .stats import qmc , bootstrap
16
+ from scipy ._lib ._util import _transition_to_rng
16
17
17
18
18
19
if TYPE_CHECKING :
@@ -61,7 +62,7 @@ def f_ishigami(x: npt.ArrayLike) -> np.ndarray:
61
62
def sample_A_B (
62
63
n : IntNumber ,
63
64
dists : list [PPFDist ],
64
- random_state : SeedType = None
65
+ rng : SeedType = None
65
66
) -> np .ndarray :
66
67
"""Sample two matrices A and B.
67
68
@@ -80,7 +81,7 @@ def sample_A_B(
80
81
:doi:`10.1016/j.cpc.2009.09.018`, 2010.
81
82
"""
82
83
d = len (dists )
83
- A_B = qmc .Sobol (d = 2 * d , seed = random_state , bits = 64 ).random (n ).T
84
+ A_B = qmc .Sobol (d = 2 * d , seed = rng , bits = 64 ).random (n ).T
84
85
A_B = A_B .reshape (2 , d , - 1 )
85
86
try :
86
87
for d_ , dist in enumerate (dists ):
@@ -246,14 +247,15 @@ def ppf(self) -> Callable[..., float]:
246
247
...
247
248
248
249
250
+ @_transition_to_rng ('random_state' , replace_doc = False )
249
251
def sobol_indices (
250
252
* ,
251
253
func : Callable [[np .ndarray ], npt .ArrayLike ] |
252
254
dict [Literal ['f_A' , 'f_B' , 'f_AB' ], np .ndarray ],
253
255
n : IntNumber ,
254
256
dists : list [PPFDist ] | None = None ,
255
257
method : Callable | Literal ['saltelli_2010' ] = 'saltelli_2010' ,
256
- random_state : SeedType = None
258
+ rng : SeedType = None
257
259
) -> SobolResult :
258
260
r"""Global sensitivity indices of Sobol'.
259
261
@@ -309,11 +311,20 @@ def sobol_indices(
309
311
The output is a tuple of the first and total indices with
310
312
shape ``(s, d)``.
311
313
This is an advanced feature and misuse can lead to wrong analysis.
312
- random_state : {None, int, `numpy.random.Generator`}, optional
313
- If `random_state` is an int or None, a new `numpy.random.Generator` is
314
- created using ``np.random.default_rng(random_state)``.
315
- If `random_state` is already a ``Generator`` instance, then the
316
- provided instance is used.
314
+ rng : `numpy.random.Generator`, optional
315
+ Pseudorandom number generator state. When `rng` is None, a new
316
+ `numpy.random.Generator` is created using entropy from the
317
+ operating system. Types other than `numpy.random.Generator` are
318
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
319
+
320
+ .. versionchanged:: 1.15.0
321
+
322
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
323
+ transition from use of `numpy.random.RandomState` to
324
+ `numpy.random.Generator`, this keyword was changed from `random_state` to `rng`.
325
+ For an interim period, both keywords will continue to work, although only one
326
+ may be specified at a time. After the interim period, function calls using the
327
+ `random_state` keyword will emit warnings.
317
328
318
329
Returns
319
330
-------
@@ -466,7 +477,7 @@ def sobol_indices(
466
477
... uniform(loc=-np.pi, scale=2*np.pi),
467
478
... uniform(loc=-np.pi, scale=2*np.pi)
468
479
... ],
469
- ... random_state =rng
480
+ ... rng =rng
470
481
... )
471
482
>>> indices.first_order
472
483
array([0.31637954, 0.43781162, 0.00318825])
@@ -587,7 +598,7 @@ def sobol_indices(
587
598
conclude on high-order terms. Hence the benefit of using Sobol' indices.
588
599
589
600
"""
590
- random_state = check_random_state (random_state )
601
+ rng = check_random_state (rng )
591
602
592
603
n_ = int (n )
593
604
if not (n_ & (n_ - 1 ) == 0 ) or n != n_ :
@@ -637,7 +648,7 @@ def indices_method(f_A, f_B, f_AB):
637
648
def wrapped_func (x ):
638
649
return np .atleast_2d (func (x ))
639
650
640
- A , B = sample_A_B (n = n , dists = dists , random_state = random_state )
651
+ A , B = sample_A_B (n = n , dists = dists , rng = rng )
641
652
AB = sample_AB (A = A , B = B )
642
653
643
654
f_A = wrapped_func (A )
0 commit comments