1010import numpy as np
1111from numpy .typing import NDArray
1212
13+ from pydvl .utils .types import Seed
14+
1315__all__ = [
1416 "running_moments" ,
1517 "num_samples_permutation_hoeffding" ,
@@ -68,24 +70,32 @@ def num_samples_permutation_hoeffding(eps: float, delta: float, u_range: float)
6870 return int (np .ceil (np .log (2 / delta ) * 2 * u_range ** 2 / eps ** 2 ))
6971
7072
71- def random_subset (s : NDArray [T ], q : float = 0.5 ) -> NDArray [T ]:
72- """Returns one subset at random from `s`.
73+ def random_subset (
74+ s : NDArray [T ],
75+ q : float = 0.5 ,
76+ seed : Optional [Seed ] = None ,
77+ ) -> NDArray [T ]:
78+ """Returns one subset at random from ``s``.
7379
7480 Args:
7581 s: set to sample from
7682 q: Sampling probability for elements. The default 0.5 yields a
7783 uniform distribution over the power set of s.
84+ seed: Either an instance of a numpy random number generator or a seed for it.
7885
7986 Returns:
8087 The subset
8188 """
82- rng = np .random .default_rng ()
89+ rng = np .random .default_rng (seed )
8390 selection = rng .uniform (size = len (s )) > q
8491 return s [selection ]
8592
8693
8794def random_powerset (
88- s : NDArray [T ], n_samples : Optional [int ] = None , q : float = 0.5
95+ s : NDArray [T ],
96+ n_samples : Optional [int ] = None ,
97+ q : float = 0.5 ,
98+ seed : Optional [Seed ] = None ,
8999) -> Generator [NDArray [T ], None , None ]:
90100 """Samples subsets from the power set of the argument, without
91101 pre-generating all subsets and in no order.
@@ -103,6 +113,7 @@ def random_powerset(
103113 Defaults to `np.iinfo(np.int32).max`
104114 q: Sampling probability for elements. The default 0.5 yields a
105115 uniform distribution over the power set of s.
116+ seed: Either an instance of a numpy random number generator or a seed for it.
106117
107118 Returns:
108119 Samples from the power set of `s`.
@@ -114,21 +125,27 @@ def random_powerset(
114125 if q < 0 or q > 1 :
115126 raise ValueError ("Element sampling probability must be in [0,1]" )
116127
128+ rng = np .random .default_rng (seed )
117129 total = 1
118130 if n_samples is None :
119131 n_samples = np .iinfo (np .int32 ).max
120132 while total <= n_samples :
121- yield random_subset (s , q )
133+ yield random_subset (s , q , seed = rng )
122134 total += 1
123135
124136
125- def random_subset_of_size (s : NDArray [T ], size : int ) -> NDArray [T ]:
137+ def random_subset_of_size (
138+ s : NDArray [T ],
139+ size : int ,
140+ seed : Optional [Seed ] = None ,
141+ ) -> NDArray [T ]:
126142 """Samples a random subset of given size uniformly from the powerset
127143 of `s`.
128144
129145 Args:
130146 s: Set to sample from
131147 size: Size of the subset to generate
148+ seed: Either an instance of a numpy random number generator or a seed for it.
132149
133150 Returns:
134151 The subset
@@ -138,11 +155,13 @@ def random_subset_of_size(s: NDArray[T], size: int) -> NDArray[T]:
138155 """
139156 if size > len (s ):
140157 raise ValueError ("Cannot sample subset larger than set" )
141- rng = np .random .default_rng ()
158+ rng = np .random .default_rng (seed )
142159 return rng .choice (s , size = size , replace = False )
143160
144161
145- def random_matrix_with_condition_number (n : int , condition_number : float ) -> NDArray :
162+ def random_matrix_with_condition_number (
163+ n : int , condition_number : float , seed : Optional [Seed ] = None
164+ ) -> NDArray :
146165 """Constructs a square matrix with a given condition number.
147166
148167 Taken from:
@@ -156,6 +175,7 @@ def random_matrix_with_condition_number(n: int, condition_number: float) -> NDAr
156175 Args:
157176 n: size of the matrix
158177 condition_number: duh
178+ seed: Either an instance of a numpy random number generator or a seed for it.
159179
160180 Returns:
161181 An (n,n) matrix with the requested condition number.
@@ -166,6 +186,7 @@ def random_matrix_with_condition_number(n: int, condition_number: float) -> NDAr
166186 if condition_number <= 1 :
167187 raise ValueError ("Condition number must be greater than 1" )
168188
189+ rng = np .random .default_rng (seed )
169190 log_condition_number = np .log (condition_number )
170191 exp_vec = np .arange (
171192 - log_condition_number / 4.0 ,
@@ -175,8 +196,8 @@ def random_matrix_with_condition_number(n: int, condition_number: float) -> NDAr
175196 exp_vec = exp_vec [:n ]
176197 s : np .ndarray = np .exp (exp_vec )
177198 S = np .diag (s )
178- U , _ = np .linalg .qr ((np . random . rand ( n , n ) - 5.0 ) * 200 )
179- V , _ = np .linalg .qr ((np . random . rand ( n , n ) - 5.0 ) * 200 )
199+ U , _ = np .linalg .qr ((rng . uniform ( size = ( n , n ) ) - 5.0 ) * 200 )
200+ V , _ = np .linalg .qr ((rng . uniform ( size = ( n , n ) ) - 5.0 ) * 200 )
180201 P : np .ndarray = U .dot (S ).dot (V .T )
181202 P = P .dot (P .T )
182203 return P
0 commit comments