33
33
from pymc .initial_point import make_initial_point_fn
34
34
from pymc .pytensorf import compile_pymc
35
35
from pymc .smc .kernels import IMH
36
- from pymc .testing import SeededTest
37
36
38
37
39
- class TestSimulator ( SeededTest ) :
38
+ class TestSimulator :
40
39
@staticmethod
41
40
def count_rvs (end_node ):
42
41
return len (
@@ -60,7 +59,6 @@ def quantiles(x):
60
59
return np .quantile (x , [0.25 , 0.5 , 0.75 ])
61
60
62
61
def setup_class (self ):
63
- super ().setup_class ()
64
62
self .data = np .random .normal (loc = 0 , scale = 1 , size = 1000 )
65
63
66
64
with pm .Model () as self .SMABC_test :
@@ -75,7 +73,7 @@ def setup_class(self):
75
73
c = pm .Potential ("c" , pm .math .switch (a > 0 , 0 , - np .inf ))
76
74
s = pm .Simulator ("s" , self .normal_sim , a , b , observed = self .data )
77
75
78
- def test_one_gaussian (self ):
76
+ def test_one_gaussian (self , seeded_test ):
79
77
assert self .count_rvs (self .SMABC_test .logp ()) == 1
80
78
81
79
with self .SMABC_test :
@@ -95,7 +93,7 @@ def test_one_gaussian(self):
95
93
assert abs (self .data .std () - po_p ["s" ].std ()) < 0.10
96
94
97
95
@pytest .mark .parametrize ("floatX" , ["float32" , "float64" ])
98
- def test_custom_dist_sum_stat (self , floatX ):
96
+ def test_custom_dist_sum_stat (self , seeded_test , floatX ):
99
97
with pytensor .config .change_flags (floatX = floatX ):
100
98
with pm .Model () as m :
101
99
a = pm .Normal ("a" , mu = 0 , sigma = 1 )
@@ -118,7 +116,7 @@ def test_custom_dist_sum_stat(self, floatX):
118
116
pm .sample_smc (draws = 100 )
119
117
120
118
@pytest .mark .parametrize ("floatX" , ["float32" , "float64" ])
121
- def test_custom_dist_sum_stat_scalar (self , floatX ):
119
+ def test_custom_dist_sum_stat_scalar (self , seeded_test , floatX ):
122
120
"""
123
121
Test that automatically wrapped functions cope well with scalar inputs
124
122
"""
@@ -149,22 +147,22 @@ def test_custom_dist_sum_stat_scalar(self, floatX):
149
147
)
150
148
assert self .count_rvs (m .logp ()) == 1
151
149
152
- def test_model_with_potential (self ):
150
+ def test_model_with_potential (self , seeded_test ):
153
151
assert self .count_rvs (self .SMABC_potential .logp ()) == 1
154
152
155
153
with self .SMABC_potential :
156
154
trace = pm .sample_smc (draws = 100 , chains = 1 , return_inferencedata = False )
157
155
assert np .all (trace ["a" ] >= 0 )
158
156
159
- def test_simulator_metropolis_mcmc (self ):
157
+ def test_simulator_metropolis_mcmc (self , seeded_test ):
160
158
with self .SMABC_test as m :
161
159
step = pm .Metropolis ([m .rvs_to_values [m ["a" ]], m .rvs_to_values [m ["b" ]]])
162
160
trace = pm .sample (step = step , return_inferencedata = False )
163
161
164
162
assert abs (self .data .mean () - trace ["a" ].mean ()) < 0.05
165
163
assert abs (self .data .std () - trace ["b" ].mean ()) < 0.05
166
164
167
- def test_multiple_simulators (self ):
165
+ def test_multiple_simulators (self , seeded_test ):
168
166
true_a = 2
169
167
true_b = - 2
170
168
@@ -214,9 +212,9 @@ def test_multiple_simulators(self):
214
212
assert abs (true_a - trace ["a" ].mean ()) < 0.05
215
213
assert abs (true_b - trace ["b" ].mean ()) < 0.05
216
214
217
- def test_nested_simulators (self ):
215
+ def test_nested_simulators (self , seeded_test ):
218
216
true_a = 2
219
- rng = self . get_random_state ( )
217
+ rng = np . random . RandomState ( 20160911 )
220
218
data = rng .normal (true_a , 0.1 , size = 1000 )
221
219
222
220
with pm .Model () as m :
@@ -244,7 +242,7 @@ def test_nested_simulators(self):
244
242
245
243
assert np .abs (true_a - trace ["sim1" ].mean ()) < 0.1
246
244
247
- def test_upstream_rngs_not_in_compiled_logp (self ):
245
+ def test_upstream_rngs_not_in_compiled_logp (self , seeded_test ):
248
246
smc = IMH (model = self .SMABC_test )
249
247
smc .initialize_population ()
250
248
smc ._initialize_kernel ()
@@ -263,7 +261,7 @@ def test_upstream_rngs_not_in_compiled_logp(self):
263
261
]
264
262
assert len (shared_rng_vars ) == 1
265
263
266
- def test_simulator_error_msg (self ):
264
+ def test_simulator_error_msg (self , seeded_test ):
267
265
msg = "The distance metric not_real is not implemented"
268
266
with pytest .raises (ValueError , match = msg ):
269
267
with pm .Model () as m :
@@ -280,7 +278,7 @@ def test_simulator_error_msg(self):
280
278
sim = pm .Simulator ("sim" , self .normal_sim , 0 , params = (1 ))
281
279
282
280
@pytest .mark .xfail (reason = "KL not refactored" )
283
- def test_automatic_use_of_sort (self ):
281
+ def test_automatic_use_of_sort (self , seeded_test ):
284
282
with pm .Model () as model :
285
283
s_k = pm .Simulator (
286
284
"s_k" ,
@@ -292,7 +290,7 @@ def test_automatic_use_of_sort(self):
292
290
)
293
291
assert s_k .distribution .sum_stat is pm .distributions .simulator .identity
294
292
295
- def test_name_is_string_type (self ):
293
+ def test_name_is_string_type (self , seeded_test ):
296
294
with self .SMABC_potential :
297
295
assert not self .SMABC_potential .name
298
296
with warnings .catch_warnings ():
@@ -303,7 +301,7 @@ def test_name_is_string_type(self):
303
301
trace = pm .sample_smc (draws = 10 , chains = 1 , return_inferencedata = False )
304
302
assert isinstance (trace ._straces [0 ].name , str )
305
303
306
- def test_named_model (self ):
304
+ def test_named_model (self , seeded_test ):
307
305
# Named models used to fail with Simulator because the arguments to the
308
306
# random fn used to be passed by name. This is no longer true.
309
307
# https://github.com/pymc-devs/pymc/pull/4365#issuecomment-761221146
@@ -323,7 +321,7 @@ def test_named_model(self):
323
321
@pytest .mark .parametrize ("mu" , [0 , np .arange (3 )], ids = str )
324
322
@pytest .mark .parametrize ("sigma" , [1 , np .array ([1 , 2 , 5 ])], ids = str )
325
323
@pytest .mark .parametrize ("size" , [None , 3 , (5 , 3 )], ids = str )
326
- def test_simulator_moment (self , mu , sigma , size ):
324
+ def test_simulator_moment (self , seeded_test , mu , sigma , size ):
327
325
def normal_sim (rng , mu , sigma , size ):
328
326
return rng .normal (mu , sigma , size = size )
329
327
@@ -357,7 +355,7 @@ def normal_sim(rng, mu, sigma, size):
357
355
358
356
assert np .all (np .abs ((result - expected_sample_mean ) / expected_sample_mean_std ) < cutoff )
359
357
360
- def test_dist (self ):
358
+ def test_dist (self , seeded_test ):
361
359
x = pm .Simulator .dist (self .normal_sim , 0 , 1 , sum_stat = "sort" , shape = (3 ,))
362
360
x = cloudpickle .loads (cloudpickle .dumps (x ))
363
361
0 commit comments